package com.app.bimo.commonbusiness.util;

/**
 * Created by limo on 16/3/1.
 */

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;


public class UiUtil {
    public static final int Top = 1;
    public static final int Bottom = 2;
    public static final int Left = 3;
    public static final int Right = 4;
    public static final int LeftRight = 5;
    private static long lastClickTime;

    public static boolean isFastDoubleClick(int dtime) {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < dtime) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


    /**
     * textView 设置 lineSpacingExtra后 5.0以下的版本  会下面多出一部分
     *
     * @param context
     * @param view
     * @param lineSpacingExtra
     */
    public static void lineSpacingExtra(Context context, TextView view, int lineSpacingExtra) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
            view.setPadding(0, 0, 0, -SystemUtil.dip2px(context, lineSpacingExtra));
        }
    }

    /**
     * 设置行间距  Android 5以前和5以后的
     *
     * @param context
     * @param lp
     * @return
     */
    public static LinearLayout.LayoutParams setLinLayoutParams(Context context, LinearLayout.LayoutParams lp, int l, int t, int r, int b) {

        if (SystemUtil.isAndroid5()) {
            lp.setMargins(SystemUtil.dip2px(context, l), SystemUtil.dip2px(context, t), SystemUtil.dip2px(context, r), SystemUtil.dip2px(context, 0));
        } else {
            lp.setMargins(SystemUtil.dip2px(context, l), SystemUtil.dip2px(context, t), SystemUtil.dip2px(context, r), SystemUtil.dip2px(context, b));
        }
        return lp;
    }

    public static void setImgToTextView(Context context, int drawId, TextView view, int position) {
        Drawable img_off;
        if (drawId == 0) {
            view.setCompoundDrawables(null, null, null, null); //设置左图标
            return;
        }
        Resources res = context.getResources();
        img_off = res.getDrawable(drawId);
        // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());

        switch (position) {

            case Top:
                view.setCompoundDrawables(null, img_off, null, null); //设置左图标
                break;
            case Bottom:
                view.setCompoundDrawables(null, null, null, img_off); //设置左图标
                break;
            case Left:
                view.setCompoundDrawables(img_off, null, null, null); //设置左图标
                break;
            case Right:
                view.setCompoundDrawables(null, null, img_off, null); //设置左图标
                break;
            default:
                break;
        }

    }

    public static void setImgToTextView(TextView view, int drawId, int position) {
        Drawable img_off;
        if (drawId == 0) {
            view.setCompoundDrawables(null, null, null, null); //设置左图标
            return;
        }
        Resources res = view.getResources();
        img_off = res.getDrawable(drawId);
        // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());

        switch (position) {

            case Top:
                view.setCompoundDrawables(null, img_off, null, null); //设置左图标
                break;
            case Bottom:
                view.setCompoundDrawables(null, null, null, img_off); //设置左图标
                break;
            case Left:
                view.setCompoundDrawables(img_off, null, null, null); //设置左图标
                break;
            case Right:
                view.setCompoundDrawables(null, null, img_off, null); //设置左图标
                break;
            default:
                break;
        }

    }


    /**
     * 设置图片在文字中
     *
     * @param view       textview
     * @param s          要给textView 设置的文字
     * @param drawableid 要给textView 设置的图片 id
     * @param start      图片画在文字中间的开始位置
     * @param end        图片画在文字中间的结束位置
     * @param left       不知道干嘛的
     * @param top        不知道干嘛的
     * @param right      图片宽
     * @param bottom     图片高
     */

    public static void setImgSpannToTextView(TextView view, String s, int drawableid, int start, int end, int left, int top, int right, int bottom) {
        if (drawableid == 0) {
            view.setText(s);
        }
        SpannableString spannableString1 = new SpannableString(s);
        Drawable drawable = view.getResources().getDrawable(drawableid);
        //设置图片的尺寸
        drawable.setBounds(left, top, right, bottom);
        ImageSpan imageSpan = new ImageSpan(drawable);
        //“表情”是占位置的，图片会把其替换掉，3和5是其索引位置，含头不含尾
        spannableString1.setSpan(imageSpan, start, end, SPAN_INCLUSIVE_EXCLUSIVE);
        view.setText(spannableString1);
    }

    /**
     * 设置图片在文字中
     *
     * @param view       textview
     * @param s          要给textView 设置的文字
     * @param drawableid 要给textView 设置的图片 id
     * @param start      图片画在文字中间的开始位置
     * @param end        图片画在文字中间的结束位置
     * @param right      图片宽
     * @param bottom     图片高
     */

    public static void setImgSpannToTextView(TextView view, String s, int drawableid, int start, int end, int right, int bottom) {
        setImgSpannToTextView(view, s, drawableid, start, end, 0, 0, right, bottom);
    }
    /**
     * 设置图片在文字中
     *
     * @param view       textview
     * @param s          要给textView 设置的文字
     */

    public static void setImgSpannToTextView(TextView view, String s) {
        setImgSpannToTextView(view, s, 0, 0, 0, 0, 0, 0, 0);
    }

    /**
     * 设置自动弹出输入框
     *
     * @param view   要弹出键盘的View
     * @param isHide 是弹出还是隐藏 true是隐藏 false是显示
     */
    public static void HiddenInpout(View view, boolean isHide) {
        InputMethodManager inputManager = (InputMethodManager) view
                .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (isHide) {
            // view.requestFocus();
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0); // 强制隐藏键盘
        } else {
            view.requestFocus();
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);

            inputManager.showSoftInput(view, 0);
        }
    }


    /**
     * 设置自动弹出输入框
     *
     * @param view   要弹出键盘的View
     * @param isHide 是弹出还是隐藏 true是隐藏 false是显示
     */
    public static void isHideInpout(View view, boolean isHide) {
        InputMethodManager inputManager = (InputMethodManager) view
                .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (isHide) {
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0); // 强制隐藏键盘
        } else {
            inputManager.showSoftInput(view, 0);
        }
    }

    /**
     * *软键盘是否已经弹出  fale  弹出   true  没弹出
     */
    public static Boolean isHideInpout(Context context) {
        if (((Activity) context).getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 文字标红
     * @param color
     * @param txt
     * @param keyword
     * @return
     */
    public static  SpannableString matchSearchText(int color, String txt, String keyword) {
        SpannableString spannableString = new SpannableString(txt);
        Pattern pattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(spannableString);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(new ForegroundColorSpan(color),
                    start,
                    end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }


}

